package cn.ssm.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import cn.ssm.pojo.OrderSeat;
import cn.ssm.service.OrderDetailService;
import cn.ssm.service.SeatService;

/**
 * 订单线程类， 检查订单状态 如到时间未付款将取消订单
 * @author 黄少涛
 *
 */
public class OrderThread implements Runnable {
	@Autowired
	private OrderDetailService orderDetailService;
	
	@Autowired
	private SeatService seatService;
	
	int orderId;
	
	public OrderThread() {
		
	}
	
	public OrderThread(int orderId) {
		super();
		this.orderId = orderId;
	}



	@Override
	public void run() {
		System.out.println("*************进入线程方法***************"+orderId);
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> params2 = new HashMap<String, Object>();
		map.put("orderId", orderId);
		param.put("orderId", orderId);
		param.put("activeId", 3);
		int num = orderDetailService.getOrderIsActiveId(map); //根据订单ID查询订单状态Id
		if(num==1) { //等于1为未付款
			 
			orderDetailService.updateOrder(param);  //更新订单状态3为取消
			List<OrderSeat> orderSeats = seatService.getOrderSeat(map); //获取订单的座位
			
			for (OrderSeat orderSeat : orderSeats) { //循环修改座位的模板
				int setId = orderSeat.getSeatId();
				String orderSeatStr =  orderSeat.getOrderSeat();
				String  newOrderSeat = orderSeatStr.replace("_", "-");
				params2.put("orderId", orderId);
				params2.put("seatId", setId);
				params2.put("orderSeat", newOrderSeat);
				seatService.updateOrderSeat(params2);
				params2.clear();
				
			}
		}
	}

}
